<?php
class Vts_GridView {
    
    private $_options;
    private $_data;
    private $_columns = array();
    private $_name;
    
    const MESSAGE_EMPTY = "List is empty.";
    
    /**
     * @var Zend_View $_view
     */
    private $_view;
    
    public function __construct($options = array()){
        $this->_options = $options;
    }
    
    public function setName($name){
        $this->_name = $name;
    }
    
    /**
     * Set data
     * @param array|Zend_Db_Table_Rowset_Abstract $data
     */
    public function setData($data){
        $this->_data = $data;
    }
    
    /**
     * add column
     * @param string $name
     * @param string $label
     * @author tien.nguyen
     */
    public function addColumn($name, $label, $options = array()){
        $this->_columns[$name] = array('label' => $label, 'options' => $options);
    }
    
    /**
    * add element columns
    * 
    * @param mixed $column
    * @param mixed $name
    * @param mixed $label
    * @param mixed $type
    */
    public function addElementColumn($column, $name, $label, $type, $options = array()){      
        if(isset($this->_columns[$column])){         
            $element = new stdClass();
            $element->name = $name;
            $element->label = $label;
            $element->type = $type;
            $element->options = $this->_convertToCodePhp($options);
           
            if(!isset($this->_columns[$column]['elements'])){
                $this->_columns[$column]['elements'] =  array();    
            }
            
            //apppend elemt
            array_push($this->_columns[$column]['elements'], $element);
        }
    }          
    
    /**
    * Convert to code php
    * @author tien.nguyen
    * 
    * @param mixed $options
    */
    private function _convertToCodePhp($options){
        foreach($options as $key => $option){            
                $options[$key] = str_replace('{', '\'.', $option);
                $options[$key] = str_replace('}', '.\'', $options[$key]);
        }
               
        return $options;
    }
    
    /**
     * remove column
     * @param string $name
     * @param string $label
     * @author tien.nguyen
     */
    public function removeColumn($name){
        unset($this->_columns[$name]);
    }
    
    /**
     * remove columns
     * @param string $name
     * @param string $label
     * @author tien.nguyen
     */
    public function removeColumns(){
        foreach($this->_columns as $key => $column){
            unset($this->_columns[$key]);
        }
    }
    
    /**
     * Set columns
     * @param array $columns
     * @author tien.nguyen
     */
    public function setColumns($columns){
        $this->_columns = $columns;
    }
    
    /**
     * Set view
     */
    public function setView($view){
        $this->_view = $view;
    }
    
    /**
     * Serialize as string
     *
     * Proxies to {@link render()}.
     *
     * @return string
     */
    public function __toString()
    {        
        try {
            $return = $this->render();
            return $return;
        } catch (Exception $e) {
            $message = "Exception caught by form: " . $e->getMessage()
                     . "\nStack Trace:\n" . $e->getTraceAsString();
            trigger_error($message, E_USER_WARNING);
            return '';
        }
    }
    
    /**
     * Render form
     *
     * @param  Zend_View_Interface $view
     * @return string
     */
    public function render(Zend_View_Interface $view = null)
    {
        $view = $this->getView();
        
        $view->addBasePath(APPLICATION_PATH.'/../library/Vts/GridView');
        $view->data = $this->_data;
        $view->columns = $this->_columns;
        $view->messageEmpty = self::MESSAGE_EMPTY;       
        
        $content = $view->render('grid-view.phtml');
        
        return $content;
    }
    
    /**
     * Retrieve view object
     *
     * If none registered, attempts to pull from ViewRenderer.
     *
     * @return Zend_View_Interface|null
     */
    public function getView()
    {
        if (null === $this->_view) {
            require_once 'Zend/Controller/Action/HelperBroker.php';
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
            $this->setView($viewRenderer->view);
        }

        return $this->_view;
    }
}